<?php
class ControllerCfNews extends Controller {
	
    /**
     * 
     * List out
     * @return unknown_type
     */
    public function index() 
    {
        // get the news list
        $this->load->model('cf/news');
        $this->data['news'] = $this->model_cf_news->getItems(array('order' => 'DESC', 'limit' => 4));
        $page = isset($this->request->get['page']) && (int)$this->request->get['page'] > 0 ? (int)$this->request->get['page'] : 1;
        
        // create pagination
        $this->load->model('tool/seo_url');
        $pagination = new Pagination();
		$pagination->total = $this->model_cf_news->getTotalNews();
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_catalog_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=cf/news&page=' . $page);		
		$this->data['pagination'] = $pagination->render();
		
		// get special products
        $this->load->model('catalog/product');
        $this->data['new_products'] = $this->model_catalog_product->getProductsOnCondition(array('special' => true, 'limit' => 3));

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/news.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/news.tpl';
		} else {
			$this->template = 'default/template/common/news.tpl';
		}        
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		
		$this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/news.css');
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }
    
    /**
     * 
     * Read news
     * @return unknown_type
     */
    public function read()
    {
        $this->load->model('cf/news');
        $news_id = (int)$this->request->get['id'];
        
        if ($news_id > 0) {
            $this->data['new'] = $this->model_cf_news->getNewsDetail($news_id);

            // get older news
            $this->data['older_news'] = $this->model_cf_news->getOlderNews($news_id);
        }
        
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/news_detail.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/news_detail.tpl';
		} else {
			$this->template = 'default/template/common/news_detail.tpl';
		}        
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));        
    }
}